package multithreading.homework.steamed;

import java.util.ArrayList;
import java.util.LinkedList;

public class Desk {
    private LinkedList a=new LinkedList<>();

    public synchronized void put() {
        try {
            String name=Thread.currentThread().getName();
            if (a.size()==0)
            {
                a.add(name + "做了一个包子");
                System.out.println(name + "做了一个包子");
                Thread.sleep(1000);
                this.notifyAll();
                this.wait();
            }

            else {
                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public synchronized void eat() {
        try {
            String name=Thread.currentThread().getName();
            if (a.size()==1){
                a.clear();
                System.out.println(name + "吃了一个包子");
                Thread.sleep(1000);

                this.notifyAll();
                this.wait();

            }
            else {

                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
